package com.ywu.application.standard.fileSys.api;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.result.CommonResult;
import com.ywu.application.standard.fileSys.dto.YwuFileSysDto;
import com.ywu.application.standard.fileSys.service.YwuFileSysServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName OpenServiceYwuFileSysApi
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/9/28 17:23
 * @Version 1.0
 **/
@RestController
@RequestMapping("/openService/ywu/file")
public class OpenServiceYwuFileSysApi {

    @Autowired
    private YwuFileSysServiceV2 service;

    /**
    * @Author GroundDemo
    * @Description 上传文件
    * @Date 9:49 2024/10/15
    * @Param [file, isShare]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.upload")
    public CommonResult<Object> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "isShare", required = false) Boolean isShare) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFileSysDto ywuFileSysDto = new YwuFileSysDto();
        ywuFileSysDto.setIsShare(isShare);
        return service.uploadFile(file, ywuFileSysDto, currentContext);
    }

    /**
    * @Author GroundDemo
    * @Description 下载文件
    * @Date 9:49 2024/10/15
    * @Param [fileId]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @GetMapping("/ywu.file.sys.download/{fileId}")
    public void downloadFile(HttpServletResponse response, @PathVariable("fileId") String fileId) {
        YwuFileSysDto ywuFileSysDto = new YwuFileSysDto();
        ywuFileSysDto.setFileId(fileId);
        service.downloadFile(response, ywuFileSysDto);
    }
}
